package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.pojo.TcCheckin;
import com.zhentao.service.TcCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/checkin")
public class CheckinController {

    @Autowired
    TcCheckinService checkinService;

    /**
     * 获取全部打卡活动
     */
    @GetMapping("list")
    public List<TcCheckin> list(){
        return checkinService.list();
    }

    /**
     * 根据ID获取打卡活动详情
     */
    @GetMapping("get/{id}")
    public TcCheckin getById(@PathVariable Integer id){
        return checkinService.getById(id);
    }

    /**
     * 添加打卡活动
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody TcCheckin checkin){
        Map<String, Object> result = new HashMap<>();
        try {
            checkin.setCreateTime(new Date());
            boolean success = checkinService.save(checkin);
            result.put("success", success);
            result.put("message", success ? "添加成功" : "添加失败");
            if (success) {
                result.put("data", checkin);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 更新打卡活动
     */
    @PutMapping("update")
    public Map<String, Object> update(@RequestBody TcCheckin checkin){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkinService.updateById(checkin);
            result.put("success", success);
            result.put("message", success ? "更新成功" : "更新失败");
            if (success) {
                result.put("data", checkin);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除打卡活动
     */
    @DeleteMapping("delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkinService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 根据班级ID获取打卡活动
     */
    @GetMapping("classCheckins/{classId}")
    public List<TcCheckin> getClassCheckins(@PathVariable Integer classId){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId);
        queryWrapper.orderByDesc("create_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 根据创建者ID获取打卡活动
     */
    @GetMapping("myCheckins/{creatorUserId}")
    public List<TcCheckin> getMyCheckins(@PathVariable Integer creatorUserId){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("creator_user_id", creatorUserId);
        queryWrapper.orderByDesc("create_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 获取进行中的打卡活动
     */
    @GetMapping("activeCheckins")
    public List<TcCheckin> getActiveCheckins(){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        Date now = new Date();
        queryWrapper.le("start_time", now);
        queryWrapper.ge("end_time", now);
        queryWrapper.orderByDesc("create_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 获取已结束的打卡活动
     */
    @GetMapping("expiredCheckins")
    public List<TcCheckin> getExpiredCheckins(){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("end_time", new Date());
        queryWrapper.orderByDesc("create_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 获取即将开始的打卡活动
     */
    @GetMapping("upcomingCheckins")
    public List<TcCheckin> getUpcomingCheckins(){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("start_time", new Date());
        queryWrapper.orderByAsc("start_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 搜索打卡活动
     */
    @GetMapping("search")
    public List<TcCheckin> searchCheckins(@RequestParam String keyword){
        QueryWrapper<TcCheckin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword).or().like("description", keyword);
        queryWrapper.orderByDesc("create_time");
        return checkinService.list(queryWrapper);
    }

    /**
     * 批量删除打卡活动
     */
    @DeleteMapping("batchDelete")
    public Map<String, Object> batchDelete(@RequestBody List<Integer> ids){
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkinService.removeByIds(ids);
            result.put("success", success);
            result.put("message", success ? "批量删除成功" : "批量删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除失败：" + e.getMessage());
        }
        return result;
    }
}
